package icu.yougan.youganblogbackend.service.impl;

import icu.yougan.youganblogbackend.mapper.UserMapper;
import icu.yougan.youganblogbackend.model.LoginRequest;
import icu.yougan.youganblogbackend.model.LoginResponse;
import icu.yougan.youganblogbackend.model.User;
import icu.yougan.youganblogbackend.service.UserService;
import icu.yougan.youganblogbackend.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<LoginResponse> login(LoginRequest loginRequest) {
        User user = userMapper.findByUsernameAndPassword(
                loginRequest.getUsername(),
                loginRequest.getPassword()
        );

        if (user == null) {
            return Result.fail("用户名或密码错误");
        }

        // Generate a mock token (in production, use JWT or other token mechanism)
        String token = UUID.randomUUID().toString().replace("-", "");

        LoginResponse loginResponse = new LoginResponse();
        loginResponse.setUserId(user.getId());
        loginResponse.setUsername(user.getUsername());
        loginResponse.setNickname(user.getNickname());
        loginResponse.setToken(token);
        loginResponse.setAvatar(user.getAvatar());
        loginResponse.setRoleId(user.getRoleId());

        return Result.success("登录成功", loginResponse);
    }
} 